from django.shortcuts import render_to_response
from django.contrib.auth.decorators import login_required
from django.contrib.auth import authenticate, login
from django.http import HttpResponseRedirect
from django.http import HttpResponse
from django.contrib.auth import logout

from inv.admin.models   import *
from inv.utils.db       import *
from inv.utils.errors   import *
from inv.utils.trace    import *

def is_admin(request):
    """function invoked by each view function to verify 
    if the logged in user is admin
    """
    if request.user.is_authenticated():
        if request.user.username == 'admin' :
            return True
    return False

def login_view(request):
    if request.method == 'GET':
        return render_to_response('admin/login.html')
    else :
        username = request.POST['USERNAME']
        password = request.POST['PASSWORD']
        user = authenticate(username=username, password=password)
        if user is not None:
            if user.is_active:
                login(request, user)
                return HttpResponseRedirect('/admin/main/')
        return HttpResponseRedirect('/admin/login/')

def logout_view(request):
    logout(request)
    return HttpResponseRedirect('/admin/login/')

def main(request):
    if is_admin(request) == False :
        return HttpResponseRedirect('/admin/login/')
    return render_to_response('admin/main.html')

def user_group(request):
    if is_admin(request) == False :
        return HttpResponseRedirect('/admin/login/')
    if request.method == 'GET':
        user_groups = User_Group.objects.all()
        return render_to_response('admin/user_group.html', 
            {'user_groups': user_groups})
    else :
        pass
    
def user_group_add(request):
    if is_admin(request) == False :
        return HttpResponseRedirect('/admin/login/')
    if request.method == 'GET':
        return render_to_response('admin/add_user_group.html')
    else :
        groupname = request.POST['GROUPNAME']
        ret = User_Group(nam=groupname).add()
        return render_to_response('ajax_response.json', ret)

# Create your views here.
def add_class(request) :
    """function for add class URL to insert a class within Class table
    """
    if request.method == 'POST':
        class_name = request.POST['CLASS_NAME']
        enabled = int(request.POST['ENB'])
        ret = Class(nam=class_name, enb=enabled).add()
        return render_to_response('ajax_response.json', ret)

def add_group(request) :
   """function for add group URL to insert a group under
   a particular Class
   """
   if request.method == 'POST':
      class_id = int(request.POST['CLASS_ID'])
      group_name = request.POST['GROUP_NAME']
      sequence = int(request.POST['SEQ'])
      enabled = int(request.POST['ENB'])
      ret = Class_Group(nam=group_name, cls_id=class_id,
         seq=sequence, enb=enabled).add()
      return render_to_response('ajax_response.json', ret)

def add_meta(request) :
   """function for add meta URL to insert a meta under
   a particular Group and Class
   """
   if request.method == 'POST':
      group_id = int(request.POST['GROUP_ID'])
      meta_name = request.POST['META_NAME']
      meta_type = request.POST['META_TYPE']
      sequence = int(request.POST['SEQ'])
      enabled = int(request.POST['ENB'])
      #Following feild is meant for NTO1 meta type only
      selection_id = request.POST['SEL_ID']
      ret = Class_Meta(nam=meta_name, grp_id=group_id, typ=meta_type,
         seq=sequence, enb=enabled, sel_id=selection_id).add()
      return render_to_response('ajax_response.json', ret)

def class_list(request, option) :
   """function to get list of classes. option is for ENABLED.DISABLED or BOTH
   """
   if request.method == 'GET':
      if option == 'BOTH' :
          classes = Class.objects.all()
      elif option == 'ENABLED' :
          classes = Class.objects.filter(enb=1)
      else :
          classes = Class.objects.filter(enb=0)
      return render_to_response('admin/class.json', {'classes' : classes})

def group_list(request, cls_id, option) :
   """function to get list of groups under a particular class. 
   option is for ENABLED.DISABLE or BOTH
   """
   if request.method == 'GET':
      if option == 'BOTH' :
          groups = Class_Group.objects.filter(cls_id=cls_id)
      elif option == 'ENABLED' :
          groups = Class_Group.objects.filter(enb=1, cls_id=cls_id)
      else :
          groups = Class_Group.objects.filter(enb=0, cls_id=cls_id)
      return render_to_response('admin/group.json', {'groups' : groups})

def meta_list(request, cls_id, grp_id, option) :
   """function to get list of metas under a particular class and group.
   if  grp_id is zero, then return all metas under this class
   option is for ENABLED.DISABLED or BOTH
   """
   if request.method == 'GET':
       if grp_id == '0' :
          if option == 'BOTH' :
              metas = Class_Meta.objects.filter(cls_id=cls_id)
          elif option == 'ENABLED' :
              metas = Class_Meta.objects.filter(cls_id=cls_id, enb=1)
          else :
              metas = Class_Meta.objects.filter(cls_id=cls_id, enb=0)
       else :
          if option == 'BOTH' :
              metas = Class_Meta.objects.filter(cls_id=cls_id, grp_id=grp_id)
          elif option == 'ENABLED' :
              metas = Class_Meta.objects.filter(cls_id=cls_id, grp_id=grp_id, enb=1)
          else :
              metas = Class_Meta.objects.filter(cls_id=cls_id, grp_id=grp_id, enb=0)
       return render_to_response('admin/meta.json', {'metas' : metas})

